//
//  LiuOriginalView.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/30.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import SnapKit
class LiuOriginalView: UIView {

    var statuse:LiustatuseViewModel? {
        didSet {
            
            if let urlStr = statuse?.homeStatuse?.user?.profile_image_url {
                headImageView.sd_setImage(with: URL(string: urlStr), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            verifiedTypeImageView.image = statuse?.verifiedTypeImage
            screenNameLabel.text = statuse?.homeStatuse?.user?.screen_name
            mBrankImageView.image = statuse?.mbrankImage
            sourceLabel.text = statuse?.sourceContent
            contentLabel.text = statuse?.homeStatuse?.text
            timeLabel.text = statuse?.timeContent
            //删除之前的底部约束
            originalBottomConstraint?.deactivate()
            
            //重新根据是否有图片添加约束
            if let picArray = statuse?.homeStatuse?.pic_urls,picArray.count > 0 {
                //有图片
                
                picView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(picView).offset(homeMargin).constraint
                })
                picView.picArray = picArray
            } else {
                //没有图片
                picView.isHidden = true
                self.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(contentLabel).offset(homeMargin).constraint
                })
            }
            
        }
    }
    
    //懒加载控件
    //  头像
    fileprivate lazy var headImageView: UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 35.0/2
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //  认证类型等级图片
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    fileprivate lazy var screenNameLabel: UILabel = UILabel(fontSize: 14, textColor: UIColor.darkGray, text: "东方不败")
    //  会员等级图片
    fileprivate lazy var mBrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    fileprivate lazy var timeLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.orange, text: "刚刚")
    //  来源
    fileprivate lazy var sourceLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.lightGray, text: "来自: 微博")
    //  微博内容
    fileprivate lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 13, textColor: UIColor.darkGray, text: "来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你来自星星的你")

        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    lazy var picView:LiuHomeRetweetPicView = {
        let pictureView = LiuHomeRetweetPicView()
        pictureView.backgroundColor = UIColor.white
        return pictureView
    }()

    //底部约束
    var originalBottomConstraint:Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        backgroundColor = UIColor.white
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mBrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picView)
        
        //自动布局
        headImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(self).offset(homeMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView).offset(homeMargin)
            make.centerY.equalTo(headImageView).offset(homeMargin)
        }
//
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(homeMargin)
        }
        
        mBrankImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(screenNameLabel)
            make.left.equalTo(screenNameLabel.snp.right).offset(homeMargin)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(homeMargin)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp.right).offset(homeMargin)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView.snp.bottom).offset(homeMargin)
            make.left.equalTo(self).offset(homeMargin)
            make.right.equalTo(self).offset(-homeMargin)
        }
        
        picView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(homeMargin).priority(.low)
            make.leading.equalToSuperview().offset(homeMargin)
            //  默认值大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //关键约束
        self.snp.makeConstraints { (make) in
            make.bottom.equalTo(picView).offset(homeMargin)
        }
    }

}
